package timing.ukulele.minio.repository;


import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import timing.ukulele.minio.mapper.MetadataInfoMapper;
import timing.ukulele.minio.model.MetadataInfoModel;
import timing.ukulele.minio.model.MetadataInfoSaveModel;
import timing.ukulele.minio.model.MetadataInfoUpdateModel;
import timing.ukulele.minio.persistent.MetadataInfoEntity;
import timing.ukulele.minio.vo.FileMetadataInfoVO;
import timing.ukulele.persistence.service.BaseService;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件元数据接口实现类
 */
@Slf4j
@Service
public class MetadataRepository extends BaseService<MetadataInfoMapper, MetadataInfoEntity> {

    /**
     * 根据条件列表查询
     *
     * @param searchDTO 查询条件
     * @return 列表结果集
     */
    public List<FileMetadataInfoVO> list(MetadataInfoModel searchDTO) {

        // 组装查询参数
        QueryWrapper<MetadataInfoEntity> queryWrapper = buildParams(searchDTO);

        List<MetadataInfoEntity> fileMetadataInfoEntityList = super.list(queryWrapper);

        List<FileMetadataInfoVO> fileMetadataInfoVoList = new ArrayList<>();

        for (MetadataInfoEntity fileMetadataInfoEntity : fileMetadataInfoEntityList) {
            FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();
            BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);
            fileMetadataInfoVoList.add(fileMetadataInfoVo);
        }

        return fileMetadataInfoVoList;
    }

    /**
     * 根据条件单条查询
     *
     * @param searchDTO 查询条件
     * @return 单条结果
     */
    public FileMetadataInfoVO one(MetadataInfoModel searchDTO) {

        // 组装查询参数
        QueryWrapper<MetadataInfoEntity> queryWrapper = buildParams(searchDTO);
        queryWrapper.last("limit 1");

        MetadataInfoEntity fileMetadataInfoEntity = super.getOne(queryWrapper);

        FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();

        if(null!=fileMetadataInfoEntity){
            BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);
        }

        return fileMetadataInfoVo;
    }

    /**
     * 新增
     *
     * @param saveDTO 数据实体
     * @return 执行结果
     */
    public FileMetadataInfoVO save(MetadataInfoSaveModel saveDTO) {

        MetadataInfoEntity fileMetadataInfoEntity = new MetadataInfoEntity();
        BeanUtils.copyProperties(saveDTO, fileMetadataInfoEntity);
        boolean result = super.save(fileMetadataInfoEntity);

        FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();
        if(result){
            BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);
        }

        return fileMetadataInfoVo;
    }

    /**
     * 修改数据
     *
     * @param updateDTO 数据实体
     * @return 执行结果
     */
    public FileMetadataInfoVO update(MetadataInfoUpdateModel updateDTO) {

        MetadataInfoEntity fileMetadataInfoEntity = new MetadataInfoEntity();
        BeanUtils.copyProperties(updateDTO, fileMetadataInfoEntity);
        boolean result = super.updateById(fileMetadataInfoEntity);

        FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();
        if(result){
            BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);
        }

        return fileMetadataInfoVo;
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 执行结果
     */
    public Boolean remove(Long id) {
        return super.removeById(id);
    }

    private QueryWrapper<MetadataInfoEntity> buildParams(MetadataInfoModel searchDTO){
        // 组装查询参数
        QueryWrapper<MetadataInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getFileKey()),"file_key",searchDTO.getFileKey());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getFileMd5()),"file_md5",searchDTO.getFileMd5());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getBucket()),"bucket",searchDTO.getBucket());
        queryWrapper.eq(null!=searchDTO.getIsPrivate(),"is_private",searchDTO.getIsPrivate());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getCreateBy()),"create_by",searchDTO.getCreateBy());

        return queryWrapper;
    }
}
